/*
 * @Author: 可以清心
 * @Description: 
 * @Date: 2023-02-14 10:11:44
 * @LastEditTime: 2023-03-08 18:32:18
 */
import md5 from 'js-md5'
import { baseAvtorUrl } from '@/service/http/config/common'

export function sleep (time = 0) {
  return new Promise(resolve => setTimeout(resolve, time))
}

/**
 * 获取设备平台
 */
export function getSystem() {
  var userAgent = navigator.userAgent.toLowerCase();
  return userAgent.indexOf('iphone') > -1 ? 'ios' : 'android'
}

/**
 * 获取设备平台
 * @returns {string}
 */
export function getPlatForm() {
  const u = navigator.userAgent
  const isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1 // android终端
  const isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) // ios终端
  let flatform = 'ios'
  if (isAndroid) {
    flatform = 'android'
  } else if (isiOS) {
    flatform = 'ios'
  }
  return flatform
}

export function ISIOS() {
  const u = navigator.userAgent
  const isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1 // android终端
  const isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) // ios终端
  let flatform = true
  if (isAndroid) {
    flatform = false
  } else if (isiOS) {
    flatform = true
  }
  return flatform
}

/**
 * @desc url传参拼接
 * @param {Object} param url上的参数
 * @returns 拼接好的url字符串
 */
export const formatUrlParam = (param) => {
  const keyList = Object.keys(param || {})
  const urlList = []

  for (const key of keyList) {
    urlList.push(`${key}=${encodeURIComponent(param[key]) || ''}`)
  }

  const urlStr = urlList.join('&')

  return urlStr
}

/**
 * @desc 根据工号（uid）生成加密头像
 * @param {*} uid 员工工号
 */
export const getUserCardUrl = (uid) => {
  let md5Uid = md5(md5(`${uid}+${uid}`))
  return `${baseAvtorUrl}/${md5Uid}.jpg`
}
